package com.jiantong.web.open.app.lawEnforcing;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jiantong.lawEnforcing.dto.CasesCheckDto;
import com.jiantong.lawEnforcing.impl.dao.CasesCheckDao;
import com.jiantong.lawEnforcing.model.CasesBasic;
import com.jiantong.lawEnforcing.model.CasesCheck;
import com.jiantong.lawEnforcing.model.PartiesInformation;
import com.jiantong.lawEnforcing.service.CasesBasicService;
import com.jiantong.lawEnforcing.service.CasesCheckService;
import com.jiantong.lawEnforcing.service.PartiesInformationService;
import com.jiantong.web.core.enums.ExceptionCode;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.common.utils.JsonMapper;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

/**
 * 案件审核
 * Created by mz03 on 2017/8/15.
 */
@Slf4j
@OpenBean
public class CasesCheckApis {
    @RpcConsumer
    private CasesCheckService casesCheckService;

    @RpcConsumer
    private PartiesInformationService partiesInformationService;

    @RpcConsumer
    private CasesBasicService casesBasicService;

    /**
     * 根据id查询一条信息
     * @param id
     * @return
     */
    @OpenMethod(
            key = "find.casesCheck.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public CasesCheck findById(Long id){
        Response<CasesCheck> response = casesCheckService.finById(id);
        if (!response.isSuccess()){
            log.error("failed to find casesCheck read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    /**
     * 根据id查询一条立案信息DTo
     * @param id
     * @return
     */
    @OpenMethod(
            key = "find.casesCheckDto.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public CasesCheckDto findCasesCheckDtoById(Long id){
        Response<CasesCheckDto> response = casesCheckService.findCasesCheckDtoById(id);
        if (!response.isSuccess()){
            log.error("failed to find CasesCheckDto read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    /**
     * 根据id查询一条立案信息DTo
     * @param cases_basic_id
     * @return
     */
    @OpenMethod(
            key = "find.casesCheckDto.by.cases_basic_id",
            paramNames = {"cases_basic_id"},
            httpMethods = RequestMethod.GET
    )
    public CasesCheckDto findByCases_basic_id(Long cases_basic_id){
        Response<CasesCheckDto> response = casesCheckService.findByCases_basic_id(cases_basic_id);
        if (!response.isSuccess()){
            log.error("failed to find CasesCheckDto read by cases_basic_id = {}, cause : {}", cases_basic_id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    /**
     * 创建一条信息
     * @param casesCheck
     * @return
     */
    @OpenMethod(
            key = "create.casesCheck",
            paramNames = {"casesCheck"},
            httpMethods = RequestMethod.POST
    )
    public Long create(CasesCheck casesCheck){
        if (casesCheck.getStatus() == null){
            casesCheck.setStatus(0);
        }
        Response<Long> response = casesCheckService.create(casesCheck);
        if(!response.isSuccess()){
            log.error("failed to create casesCheck read by casesCheck = {}, cause : {}", casesCheck, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 更新单据
     */
    @OpenMethod(
            key = "update.casesCheck.answer",
            paramNames = {"casesCheck"},
            httpMethods = RequestMethod.PUT
    )
    public Boolean update(CasesCheck casesCheck){
        if(casesCheck.getId() == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<Boolean> response = casesCheckService.update(casesCheck);
        if(!response.isSuccess()){
            log.error("failed to update casesCheck");
            throw new OPServerException(ExceptionCode.DATA_UPDATE_FAIL.value(),
                    "failed to update casesCheck");
        }
        return response.getResult();
    }

    /**
     * 根据条件分页查询
     * @param pageNo
     * @param pageSize
     * @param statusList
     * @param is_succeed
     * @param cause_action
     * @param cases_source
     * @param parties_type
     * @param parties_name
     * @return
     */
    @OpenMethod(
            key = "find.paging.casesCheck",
            paramNames = {"pageNo","pageSize","statusList","is_succeed","cause_action","cases_source","parties_type","parties_name"},
            httpMethods = RequestMethod.GET
    )
    public Paging<CasesCheck> pagingCasesCheck(
            Integer pageNo,
            Integer pageSize,
            List<Integer> statusList,
            Integer is_succeed,
            String cause_action,
            Integer cases_source,
            Integer parties_type,
            String parties_name
    ){
        Response<Paging<CasesCheck>> pagingResponse =casesCheckService.pagingByStatus(pageNo,pageSize,statusList,is_succeed,cause_action,cases_source,parties_type,parties_name);
        if(!pagingResponse.isSuccess()){
            log.error("failed to find paging casesBasic status {}, cause : {}", statusList, pagingResponse.getError());
            throw new OPServerException(pagingResponse.getError());
        }
        return pagingResponse.getResult();
    }



    /**
     * 根据条件分页查询casesCheckDto
     * @param pageNo
     * @param pageSize
     * @param statusList
     * @param is_succeed
     * @param cause_action
     * @param cases_source
     * @param parties_type
     * @param parties_name
     * @return
     */
    @OpenMethod(
            key = "find.paging.casesCheckDto",
            paramNames = {"pageNo","pageSize","statusList","is_succeed","cause_action","cases_source","parties_type","parties_name"},
            httpMethods = RequestMethod.GET
    )
    public Paging<CasesCheckDto> pagingCasesCheckDto(
            Integer pageNo,
            Integer pageSize,
            List<Integer> statusList,
            Integer is_succeed,
            String cause_action,
            Integer cases_source,
            Integer parties_type,
            String parties_name
    ){
        try {
            Paging<CasesCheck> paging = pagingCasesCheck(pageNo,pageSize,statusList,is_succeed,cause_action,cases_source,parties_type,parties_name);
            List<CasesCheck> list = paging.getData();
            List<CasesCheckDto> casesCheckDtoList = new ArrayList<>();
            for (CasesCheck casesCheck : list) {
                CasesCheckDto casesCheckDto = new CasesCheckDto();
                CasesBasic casesBasic = casesBasicService.finById(casesCheck.getCases_basic_id()).getResult();
                PartiesInformation partiesInformation = partiesInformationService.finById(casesBasic.getParties_id()).getResult();
                casesCheckDto.setPartiesInformation(partiesInformation);
                casesCheckDto.setCasesBasic(casesBasic);
                casesCheckDto.setCasesCheck(casesCheck);
                casesCheckDtoList.add(casesCheckDto);
            }
            Paging<CasesCheckDto> casesCheckDtoPaging = new Paging<>(paging.getTotal(), casesCheckDtoList);
            return casesCheckDtoPaging;
        }catch (Exception e){
            log.error("failed to find paging casesBasicDto status {}", statusList);
            throw new OPServerException("failed to find paging casesBasicDto");
        }
    }
}
